/// <reference path="../utilities/FXDom.js" />
/// <reference path="../utilities/Markup.js" />
/// <reference path="../utilities/Post.js" />
/// <reference path="../utilities/Xpcom.js" />
///--------------------------------------------------------------------
/// <file name="QuickMove.js">
///   Provides enhancements for quickly moving threads.
/// </file>
///--------------------------------------------------------------------

NAMFox.ns(function() { with (NAMFox) {

this.QuickMove = function(_$, htmlDocument, thread) {
    this._$ = _$;
    this.htmlDocument = htmlDocument;
    this.thread = thread;
};

QuickMove.targetForumId = 47;
QuickMove.getCookieCrumbMarkup = function(htmlDocument, callback) {
    var cookieCrumb = $FX("#cookiecrumbtext", htmlDocument);
    var cookieCrumbHtml = '';
    
    var chevron = String.fromCharCode(187);

    if (!cookieCrumb.length) {
        cookieCrumb = $FX("div.cookiecrumb", htmlDocument);

        cookieCrumbHtml = cookieCrumb.html();
        cookieCrumbHtml = cookieCrumbHtml.substring(cookieCrumbHtml.indexOf(chevron) + 2);
    }
    else {
        cookieCrumbHtml = cookieCrumb.html();
    }

    cookieCrumbHtml = cookieCrumbHtml.substring(0, cookieCrumbHtml.lastIndexOf(chevron) - 1);

    function markupReceived(markup) {
        callback(markup);
    }

    MarkupTranslator.getMarkupFromHtml(htmlDocument, cookieCrumbHtml,  markupReceived);
};

QuickMove.prototype = {
    attach: function() {
        // Add the move thread to Evidence button.
        var moveThreadLink = this._$("a:contains(move thread)");
        
        if (this.thread.isUserModerator() && moveThreadLink.length > 0) {
            $.verbose("VIEW THREAD | Adding move thread to evidence link.");
            
            var dialog = '';
            dialog += '<div id="namfox-move-thread-to-evidence">'
            dialog += '  <form';
            dialog += '    id="namfox-move-thread-to-evidence-form"';
            dialog += '    method="POST"';
            dialog += '    action="' + $.url.forum("/index.php") + '"';
            dialog += '    enctype="application/x-www-form-urlencoded">';
            dialog += '    <p><strong>(Optional) Provide additional comments before moving the thread.</strong></p>';
            dialog += '    <textarea id="namfox-move-thread-to-evidence-textarea" name="pst_body" rows="5" cols="50"></textarea>';
            dialog += '    <input type="hidden" name="pst_subject" value="' + $.attrEncode(this.thread.getSubjectForReply()) + '" />';
            dialog += '    <input type="hidden" name="oldforumid" value="' + this.thread.forumId + '" />';
            dialog += '    <input type="hidden" name="threadid" value="' + this.thread.threadId + '" />';
            dialog += '    <input type="hidden" name="manual_newforumid" value="' + QuickMove.targetForumId + '" />';
            dialog += '    <input id="namfox-move-thread-to-evidence-form-submit" type="submit" name="submit" value="true" style="display: none;" />';
            dialog += '    <input type="hidden" name="function" value="move_thread" />';
            dialog += '    <input type="hidden" name="mark_moved" value="true" />';
            dialog += '    <input type="hidden" name="restore_reported_posts" value="true" />';
            dialog += '  </form>';
            dialog += '</div>';
            
            moveThreadLink.after(' &nbsp; <a class="namfox-post-link">move thread to evidence</a>');
            
            this._$("body").append(dialog);

            // Leverage the jQuery and jqModal plugins already in use
            // by this page in order to create the dialog, but use them
            // in a sandbox to restrict privileges.
            var jQuery = this.htmlDocument.defaultView.wrappedJSObject.$;
            var sandbox = new Components.utils.Sandbox(this.htmlDocument.defaultView);
            sandbox.jQuery = jQuery;
            this._$("a:contains(move thread to evidence)").click(
                function() {
                    // Warning: A dangerous or banned global object was accessed by some JavaScript code.
                    // Warning: Accessed object: evalInSandbox
                    // Here we only execute code and do not rely on return values inside our chrome-privileged script.
                    Components.utils.evalInSandbox(
                        'jQuery("#namfox-move-thread-to-evidence").dialog("open");',
                        sandbox);
                }
            );
            
            var dialogOptionsInit = '';
            dialogOptionsInit += 'var dialogOptions = {';
            dialogOptionsInit += '    height: 275,';
            dialogOptionsInit += '    width: 500,';
            dialogOptionsInit += '    autoOpen: false,';
            dialogOptionsInit += '    modal: true,';
            dialogOptionsInit += '    overlay: {';
            dialogOptionsInit += '        backgroundColor: "#E0E0E0",';
            dialogOptionsInit += '        opacity: 0.7';
            dialogOptionsInit += '    },';
            dialogOptionsInit += '    title: "Move Thread to Evidence",';
            dialogOptionsInit += '    buttons: {';
            dialogOptionsInit += '        "Move Thread": function() {';
            dialogOptionsInit += '            jQuery("#namfox-move-thread-to-evidence-form-submit").click();';
            dialogOptionsInit += '        },';
            dialogOptionsInit += '        "Cancel":  function() {';
            dialogOptionsInit += '            jQuery("#namfox-move-thread-to-evidence").dialog("close")';
            dialogOptionsInit += '        }';
            dialogOptionsInit += '    }';
            dialogOptionsInit += '};';
            
            // Warning: A dangerous or banned global object was accessed by some JavaScript code.
            // Warning: Accessed object: evalInSandbox
            // Here we only execute code and do not rely on return values inside our chrome-privileged script.
            Components.utils.evalInSandbox(
                dialogOptionsInit + 'jQuery("#namfox-move-thread-to-evidence").dialog(dialogOptions);',
                sandbox);

            var that = this;
            function cookieCrumbReceived(cookieCrumbMarkup) {
                that._$("#namfox-move-thread-to-evidence-textarea").val("Moved from " + cookieCrumbMarkup);
            }

            QuickMove.getCookieCrumbMarkup(this.htmlDocument, cookieCrumbReceived);
        }
    }
};

}});